import init from '@feidao/web/framework/browser/d/component';
import { id, spaceid, systemid, refsystemid } from './config';
import { IFeidaoAiBrowserComponent } from '@feidao/web/interfaces';

import storage_set from '@feidao/web/atom/browser/storage/put';
import storage_get from '@feidao/web/atom/browser/storage/get';

import hide from '../../atom/hide';
import show from '../../atom/show';
import nodejs from '@feidao/web/atom/browser/msg/nodejs';
import fire from '@feidao/web/atom/browser/msg/fire';

const actions = {
	cmpt_show(fd: IFeidaoAiBrowserComponent, ...args: any[]) {

		fd.data.node.querySelector('[data-feidao-zjc="05"]').classList.remove('display-n')
		return show(fd.data.node);

	},
	close_cmpt(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		return hide(fd.data.node);

	},
	change_role_no(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		// console.log(args);
		const target_node = args[0].target;
		const role_no = target_node.getAttribute('data-feidao-role_no');
		// console.log(role_no);
		return storage_set('role_no', role_no);
	},
	async role_sure(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		const role_no = await storage_get('role_no');
		if (!role_no) {
			fire('zjc-000002', 'ec-001', 'fail', '请选择角色');
			return;
		}
		const res = await nodejs(fd.data.spaceid, 'web/change-role', { role_no }, false, false);
		if (res['result']) {// 获取该角色下的首页菜单
			const url = await nodejs(fd.data.spaceid, 'user_jump', { data: { role_no } }, false, false);
			if (url) {
				(window as any).location.href = url;
			} else {
				fire('zjc-000002', 'ec-001', 'fail', '该角色下没有首页');
			}
		} else {
			fire('zjc-000002', 'ec-001', 'fail', '切换角色失败');
		}
	}
};

const s = {
	'cmpt_show': 'cmpt_show',
	'close_cmpt': 'close_cmpt',
	'role_sure': 'role_sure',
	'change_role_no': 'change_role_no'
};

export default function main(url: string, query: any) {
	return init(id, s, actions, url, query, { spaceid, systemid, refsystemid });
}
